package com.kinon.bs_app.activities;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.kinon.bs_app.R;
import com.kinon.bs_app.adapter.FragmentAdapter;
import com.kinon.bs_app.fragments.DownMsgFragment;
import com.kinon.bs_app.fragments.UpMsgFragment;
import com.kinon.bs_app.utils.BaseActivity;

import java.util.ArrayList;
import java.util.List;

public class MessageActivity extends BaseActivity implements View.OnClickListener {

    private Button btn_back = null;

    private TextView tv_upMsg = null;
    private TextView tv_downMsg = null;

    private ViewPager vp_view = null;

    private FrameLayout fl_frame = null;
    private List<Fragment> fragments = new ArrayList<>();
    private FragmentManager fragmentManager = null;
//    private FragmentAdapter fragmentAdapter = null;

    private int currentItem = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        initView();
        initListener();
    }

    private void initView() {
        btn_back = findViewById(R.id.btn_back);

        tv_upMsg = findViewById(R.id.tv_upMsg);
        tv_downMsg = findViewById(R.id.tv_downMsg);

        vp_view = findViewById(R.id.vp_view);

        addFragments();


//        fragmentAdapter = new FragmentAdapter(this.getSupportFragmentManager(), fragments);
//        vp_view.setOffscreenPageLimit(2);  // ViewPager缓存设为2帧
//        vp_view.setAdapter(fragmentAdapter);
//        vp_view.setCurrentItem(0);

    }

    private void initListener() {
        btn_back.setOnClickListener(this);
        tv_upMsg.setOnClickListener(this);
        tv_downMsg.setOnClickListener(this);
//        vp_view.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int i, float v, int i1) {
//
//            }
//
//            @Override
//            public void onPageSelected(int i) {
//
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int i) {
//
//            }
//        });
    }

    private void addFragments() {
        fragments.add(new UpMsgFragment());
        fragments.add(new DownMsgFragment());
        showFirstFragment();
    }

    private void showFirstFragment() {
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fl_frame, fragments.get(currentItem));
        fragmentTransaction.show(fragments.get(currentItem));
        fragmentTransaction.commit();
    }

    private void showFragment(int position) {
        if (currentItem != position) {
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            //隐藏之间显示的fragment
            fragmentTransaction.hide(fragments.get(currentItem));
            //判断fragment是否加载过，
            if (!fragments.get(position).isAdded()) {
                fragmentTransaction.add(R.id.fl_frame, fragments.get(position));
            }
            fragmentTransaction.show(fragments.get(position));
            fragmentTransaction.commit();
            currentItem = position;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.tv_upMsg:
                tv_upMsg.setBackgroundColor(getResources().getColor(R.color.dark_blue));
                tv_upMsg.setTextColor(getResources().getColor(R.color.white));
                tv_downMsg.setBackgroundColor(getResources().getColor(R.color.white));
                tv_downMsg.setTextColor(getResources().getColor(R.color.dark_blue));
                showFragment(0);
                break;
            case R.id.tv_downMsg:
                tv_downMsg.setBackgroundColor(getResources().getColor(R.color.dark_blue));
                tv_downMsg.setTextColor(getResources().getColor(R.color.white));
                tv_upMsg.setBackgroundColor(getResources().getColor(R.color.white));
                tv_upMsg.setTextColor(getResources().getColor(R.color.dark_blue));
                showFragment(1);
                break;
            default:
                break;
        }
    }
}
